<template>
  <el-row type="flex" style="height: 40px ; width: 100%">
    <el-col><span>{{ treeNode.name }}</span></el-col>
    <el-col :span="4">
      <el-row type="flex">
        <span style="paddingRight: 10px">{{ treeNode.manager }}</span>
        <el-dropdown @command="operater">
          <span class="el-dropdown-link">
            操作<i class="el-icon-arrow-down" />
          </span>
          <el-dropdown-menu slot="dropdown">
            <!-- 下拉选项 -->
            <el-dropdown-item command="add">添加子部门</el-dropdown-item>
            <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
            <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  name: '',
  components: {},
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {}
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    operater(type) {
      if (type === 'add') {
        // 添加部门名称
        // 告诉父组件 显示弹层
        this.$emit('addDepts', this.treeNode) // 为何传出treeNode 因为是添加子部门 需要当前部门的数据
      } else if (type === 'edit') {
        // 编辑部门
        this.$emit('editDepts', this.treeNode)
      } else {
        console.log('删除')
        this.$confirm('是否删除此条记录？').then(() => {
          return delDepartments(this.treeNode.id)
        }).then(res => {
          // 重新获取列表数据
          this.$message('删除成功')
          this.$emit('getDepartments')
        }).catch(err => {
          console.log(err)
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
